package cn.com.self.apis.portal.controller;

import cn.com.self.apis.portal.entity.TransactionRecordEntity;
import cn.com.self.apis.portal.service.intf.ITransactionRecordService;
import cn.com.self.apis.portal.util.ICustomPage;
import cn.com.self.apis.util.CopyUtils;
import cn.com.selfsoft.smartcity.commons.http.DataItem;
import cn.com.selfsoft.smartcity.commons.http.DataPage;
import cn.com.selfsoft.smartcity.commons.http.ResultModel;
import cn.com.selfsoft.smartcity.commons.util.ParameterUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author CRL
 * @date 2021/1/9 16:23
 */
@Controller
@RequestMapping("/transaction")
@Api(tags = "交易记录")
public class TransactionRecordController {

    @Autowired
    ITransactionRecordService transactionRecordService;

    @PostMapping(value = "/create")
    @ApiOperation(value = "创建交易记录")
    public ResponseEntity create(@RequestBody TransactionRecordEntity entity) {
        TransactionRecordEntity transactionRecordEntity = this.transactionRecordService.create(entity);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(new DataItem(transactionRecordEntity)));
    }

    @DeleteMapping(value = "/delete/{id}")
    @ApiOperation(value = "删除交易记录")
    public ResponseEntity deletes(@PathVariable("id") String id) {
        this.transactionRecordService.delete(id);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(1));
    }

    @PostMapping(value = "/update/{id}")
    @ApiOperation(value = "更新交易记录")
    public ResponseEntity update(@PathVariable("id") String id, @RequestBody TransactionRecordEntity entity) {
        TransactionRecordEntity transactionRecordEntity = this.transactionRecordService.load(id);
        CopyUtils.copyProperties(entity, transactionRecordEntity);
        TransactionRecordEntity info = this.transactionRecordService.update(transactionRecordEntity);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(new DataItem(info)));
    }

    @GetMapping(value = "/page")
    @ApiOperation(value = "分页获取交易记录")
    public ResponseEntity getUserPage(HttpServletRequest request, Pageable page) {
        Map<String, Object> queryParams = ParameterUtil.HumpToUnderline(request.getParameterMap(), "mark", "size", "page", "sort","access_token");
        ICustomPage<TransactionRecordEntity> pageData = this.transactionRecordService.page(queryParams, page);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(pageData));
    }

    @GetMapping(value = {"/load/{id}"})
    @ApiOperation(value = "根据id获取交易记录")
    public ResponseEntity loadId(@PathVariable("id") String id, HttpServletRequest request) {
        TransactionRecordEntity transactionRecordEntity = this.transactionRecordService.load(id);
        return ResponseEntity.status(HttpStatus.OK).body(new cn.com.self.apis.core.vo.ResultModel<>(transactionRecordEntity));
    }

}
